<?php
require_once 'Db/Db_Db.php';
class Application_Model_Data {
	
	public function getQuestions() {
		
		//get level = 1,2,3
		$results = array();
		$setresults = array();
		$i=1;
		try{
			while ($i<=3){
				$total = 0;
				$form = 0;
				if($i==1){
					$results =	$this->getQLevel($i);
					$form = 0;
					foreach ($results as $result) {
						$setresults[$form] = $result;
						$form++;
					}
					$results = null;
				}
				if($i==2){
					$results =	$this->getQLevel($i);
					$form = 5;
					foreach ($results as $result) {
						$setresults[$form] = $result;
						$form++;
					}
					$results = null;
				}
				if($i==3){
					$results =	$this->getQLevel($i);
					$form = 10;
					foreach ($results as $result) {
						$setresults[$form] = $result;
						$form++;
					}
					$results = null;
				}
				$i++;
			}
		}
		catch(Zend_Db_Exception $e){
		     return null;
		 }
		
		return $setresults;

	}
	
	/**
	 * get 5 easy question
	 */
	public function getQuestionData($level = NULL){
		
			$limit_question = 5;

		//Create COnnect Db
		$db = new Db_Db ( );
		$get_result = array();
		
		try{
			$conn = $db->conn_db();
			$query ="SELECT * FROM questions WHERE 'level_id' =$level LIMIT 0 , $limit_question";

			$result = mysqli_query($conn,$query) or die(mysqli_error($conn));

			if(mysqli_num_rows($result) == 0){
					return array('name'=>"Data empty");
			}
			else{
				//var_dump($result);
				$set_questions = array();
				$i =0;
				while($row = mysqli_fetch_array($result,MYSQLI_ASSOC)){
						$set_questions[$i]['id']			=  $row['id'];
						$set_questions[$i]['level_id']		=  $row['level_id'];
						$set_questions[$i]['name']			=  trim($row['name']);
						$set_questions[$i]['question_a']	=  trim($row['question_a']);
						$set_questions[$i]['question_b']	=  trim($row['question_b']);
						$set_questions[$i]['question_c']	=  trim($row['question_c']);
						$set_questions[$i]['question_d']	=  trim($row['question_d']);
						$set_questions[$i]['answer']		=  $row['answer'];
						$i++;
				}
			}

		}
		catch(Zend_Db_Exception $e){
		      //echo 'Error: '.$e->getMessage();
		      return null;
		   }
		return $set_questions;
	}

	//-----------------------------------------------------------
	/**
	 * Get Quesstion method
	 * 
	 * @return Array
	 */
	public function questionData($num) {
		
		if (!isset ( $num ))
			$num = 10;
		else if(is_array($num))
			$get_num = $num[0];
		$data_qs = array ();
		for($a = 0; $a < $get_num; $a ++) {
			$rand = mt_rand ( 2, 5 );
			$rand = $rand - 1;
			$data_qs [] = array (
				'name' => 'These days everbody is aware .......... the danger of smoking. ', 
				'question_a' => '1',
				 'question_b' => '2', 
				 'question_c' => '3', 
				 'question_d' => '4', 
				 'answer' => '' . $rand . '', 
				 'created' => date ( 'Y-m-d H:i:s' ),
				 'modified' => date ( 'Y-m-d H:i:s' ) 
			);
		}
		return $data_qs;
	}

	//-----------------------------------------------------------
	/**
	 * Test method
	 * 
	 * @return string
	 */
	public function test() {
		return 'Hello XMLRPC!';
	}

	public function getQLevel($level) {
		//return "vinh 12 $level $form";
		if (!isset ( $level ))
			$level = 1;
		$oderbyquestion = array(
			0=>'user_id',
			1=>'id',
			2=>'levle_id',
			3=>'name',
			4=>'question_a',
			5=>'question_b',
			6=>'question_c',
			7=>'question_d',
			8=>'answer',
			9=>'category_id',
			10=>'created',
			11=>'modified'
		);
		$setrandtitle = mt_rand(1,11);
		if($setrandtitle == null)
			$setrandtitle = 3;
		$setorder = $oderbyquestion[$setrandtitle];
		$oderby = array(0=>'ASC',1=>'DESC',2=>'DESC');
		$setrandoderby = mt_rand(1,2);
		if($setrandoderby == null)
			$setrandoderby = 1;
		$setorder = $oderbyquestion[$setrandtitle];
		$setoderby = $oderby[$setrandoderby];
		//Create COnnect Db
		$db = new Db_Db ( );
		$get_result = array();
		try{
			$conn = $db->conn_db();
			$query ="SELECT * FROM questions where level_id= $level ORDER BY $setorder $setoderby LIMIT 0 , 5;";
			$result = mysqli_query($conn,$query) or die(mysqli_error($conn));

			if(mysqli_num_rows($result) == 0){
					return array('name'=>"Data empty");
			}
			else{
				//var_dump($result);
				$set_questions = array();
				$i=0;
				while($row = mysqli_fetch_array($result,MYSQLI_ASSOC)){
						$set_questions[$i]['id']			=  $row['id'];
						$set_questions[$i]['level_id']		=  $row['level_id'];
						$set_questions[$i]['name']			=  trim($row['name']);
						$set_questions[$i]['question_a']	=   trim($row['question_a']);
						$set_questions[$i]['question_b']	=   trim($row['question_b']);
						$set_questions[$i]['question_c']	=   trim($row['question_c']);
						$set_questions[$i]['question_d']	=   trim($row['question_d']);
						$set_questions[$i]['answer']		=  $row['answer'];
						$i++;
				}
			}

		}
		catch(Zend_Db_Exception $e){
		      echo 'Error: '.$e->getMessage();
		   }
		$setrandoderby = $setrandtitle = null;   
		return $set_questions;
	}
	
}


